package com.swt.gap.service.impl;

import com.alibaba.fastjson.JSON;
import com.swt.gap.common.graph.Graph;
import com.swt.gap.config.ServerConfig;
import com.swt.gap.dao.TaskDao;
import com.swt.gap.dao.TaskResultMapper;
import com.swt.gap.model.entity.Task;
import com.swt.gap.model.entity.TaskResultWithBLOBs;
import com.swt.gap.model.vo.TaskResultVO;
import com.swt.gap.model.vo.taskresult.TaskAndResult;
import com.swt.gap.service.AchievementTaskResultService;
import com.swt.gap.util.UserLoginUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;

/**
 * @Author tyh
 * @Date 2020/5/11 20:50
 */

@Service
public class AchievementTaskResultServiceImpl implements AchievementTaskResultService {

    private final TaskDao taskDao;
    private final TaskResultMapper taskResultMapper;
    private final ServerConfig serverConfig;

    public AchievementTaskResultServiceImpl(TaskDao taskDao, TaskResultMapper taskResultMapper,
                                            ServerConfig serverConfig) {
        this.taskDao = taskDao;
        this.taskResultMapper = taskResultMapper;
        this.serverConfig = serverConfig;
    }


    @Override
    public List<TaskResultVO> getTaskResults(String keyword) {
        List<TaskResultVO> retValue = new ArrayList<>();
        Integer createdBy = UserLoginUtils.getUser().getUserId();

        List<Task> tasks = taskDao.getAccessTask(null, createdBy);
        tasks.forEach(task -> {
            List<TaskResultWithBLOBs> results = taskResultMapper.selectResultByTaskAndName(task.getTaskId(), keyword);
            if (!CollectionUtils.isEmpty(results)) {
                TaskResultVO taskResult = new TaskResultVO();
                taskResult.setTaskId(task.getTaskId());
                taskResult.setTaskName(task.getTaskName());

                List<TaskAndResult> taskResults = new ArrayList<>();
                results.forEach(r -> {
                    TaskAndResult tr = new TaskAndResult();
                    BeanUtils.copyProperties(r, tr);
                    Set<String> fields = new HashSet<>();
                    String content = r.getContent();
                    List<Graph.Node> nodes = new ArrayList<>();
                    switch (r.getType()) {
                        case 1:
                            nodes = JSON.parseObject(content, Graph.class).getNodes();
                            break;
                        case 2:
                            nodes = JSON.parseArray(content, Graph.Node.class);
                            break;
                        default:
                            break;
                    }
                    nodes.forEach(node -> {
                        if (node.getLabels().contains("事件")) {
                            fields.add("事件类型");
                        } else {
                            fields.add(node.getLabels().get(1));
                        }
                    });
                    tr.setStatisticsFields(fields);
                    tr.setContent("");


                    tr.setPicSvg(serverConfig.parseViewUrl(r.getPicSvg(), 2));
                    taskResults.add(tr);

                });
                taskResult.setTaskResults(taskResults);
                retValue.add(taskResult);
            }
        });
        /*List<TaskAndResult> taskAndResults = taskMapper.selectHasResultTask(createdBy,keyword);
        Map<Integer, List<TaskAndResult>> mapTaskAndResults = taskAndResults.stream()
                .collect(Collectors.groupingBy(TaskAndResult :: getTaskId));
        mapTaskAndResults.forEach((k,v) -> {
            TaskResultVO taskResult = new TaskResultVO();
            taskResult.setTaskId(k);
            taskResult.setTaskName(v.get(0).getTaskName());

            Set<String> fields = new HashSet<>();
            v.forEach(taskAndResult -> {
//                String content = taskAndResult.getContent().replace(",\"x\":-{", "},{");
                String content = taskAndResult.getContent();
                List<Graph.Node> nodes = new ArrayList<>();
                 *//*if (taskAndResult.getType().equals(1)) {
                    nodes = JSON.parseObject(content, Graph.class).getNodes();
                } else if (taskAndResult.getType().equals(2)) {
                    nodes = JSON.parseArray(content, Graph.Node.class);
                }*//*
                switch (taskAndResult.getType()) {
                    case 1:
                        nodes = JSON.parseObject(content, Graph.class).getNodes();
                        break;
                    case 2:
                        nodes = JSON.parseArray(content, Graph.Node.class);
                        break;
                    default:
                        break;
                }
                nodes.forEach(node -> {
                    if (node.getLabels().contains("事件")) {
                        fields.add("事件类型");
                    } else {
                        fields.add(node.getLabels().get(1));
                    }
                });
                taskAndResult.setStatisticsFields(fields);
                taskAndResult.setContent("");
            });

            taskResult.setTaskResults(v);
            retValue.add(taskResult);
        });*/

        return retValue;
        /*Map<Integer,TaskResultVO> map = new HashMap<>();
        if (taskAndResults != null && taskAndResults.size() > 0){
            for (TaskAndResult taskAndResult : taskAndResults){
                TaskResultWithBLOBs taskResultWithBLOBs = new TaskResultWithBLOBs();
                taskResultWithBLOBs.setResultId(taskAndResult.getResultId());
                taskResultWithBLOBs.setResultName(taskAndResult.getResultName());
                taskResultWithBLOBs.setDescription(taskAndResult.getDescription());
                taskResultWithBLOBs.setPicSvg(taskAndResult.getPicSvg());
                taskResultWithBLOBs.setType(taskAndResult.getType());
                taskResultWithBLOBs.setCreatedBy(taskAndResult.getCreatedBy());
                taskResultWithBLOBs.setCreatedTime(taskAndResult.getCreatedTime());
                taskResultWithBLOBs.setUpdateTime(taskAndResult.getUpdateTime());
                taskResultWithBLOBs.setIsDeleted(taskAndResult.getIsDeleted());
                taskResultWithBLOBs.setTaskId(taskAndResult.getTaskId());


                int taskId = taskAndResult.getTaskId();
                TaskResultVO taskResultVO = map.get(taskId);
                if (taskResultVO == null){
                    List<TaskResultWithBLOBs> newTaskResultWithBLOBList = new ArrayList<>();
                    TaskResultVO newTaskResultVO = new TaskResultVO();
                    newTaskResultVO.setTaskId(taskAndResult.getTaskId());
                    newTaskResultVO.setTaskName(taskAndResult.getTaskName());
                    newTaskResultWithBLOBList.add(taskResultWithBLOBs);
                    newTaskResultVO.setTaskResults(newTaskResultWithBLOBList);
                    map.put(taskAndResult.getTaskId(),newTaskResultVO);
                }else {
                    List<TaskResultWithBLOBs> taskResultWithBLOBList = taskResultVO.getTaskResults();
                    taskResultWithBLOBList.add(taskResultWithBLOBs);
                }
            }
        }*/

        /*List<TaskResultVO> list = new ArrayList<>();
        map.forEach((x,y) ->{
            list.add(y);
        });

        return list.stream().sorted(Comparator.comparing(TaskResultVO::getTaskId).reversed()).collect(Collectors.toList());
*/
        /*List<TaskResultVO> taskResultVOS = new ArrayList<>();
        List<Task> taskList = taskMapper.getTaskList(createdBy);
        //取任务下的分析结果
        for (Task task : taskList) {
            TaskResultVO taskResultVO = new TaskResultVO();
            taskResultVO.setTaskId(task.getTaskId());
            taskResultVO.setTaskName(task.getTaskName());
            taskResultVO.setTaskResults(taskResultMapper.getResults(task.getTaskId()));
            taskResultVOS.add(taskResultVO);
        }

        return collect;*/
    }

}
